from string import ascii_letters, digits, punctuation
from itertools import *
import mysql.connector
from mysql.connector import errorcode
from optparse import OptionParser
import hashlib,binascii
import re
import datetime, platform


def block():
    #cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='hash')
    cursor = cnx.cursor()
    query = "SELECT idmd5, start, stop, keyl FROM md5 WHERE Done = 'false' LIMIT 1"
    cursor.execute (query)
    for (idmd5, start, stop, keyl) in cursor:
        g = ("{},{}, {}, {}" .format(idmd5, start, stop, keyl))
    current_time = str(datetime.datetime.now())
    update_time = "UPDATE md5 SET Date_Start = '%s' WHERE idmd5 = %d" % (current_time, idmd5)
    print (update_time)
    cursor.execute (update_time)
    cnx.commit()
    cursor.close()
    return start, stop, idmd5, keyl


def update_block(block_id, stat):
    #cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='hash')
    cursor = cnx.cursor()
    Status = stat
    current_time = str(datetime.datetime.now())
    machined = str(platform.node())
    #print (machined)
    
    sqltime = "UPDATE md5 SET Done = '%s', Date_End = '%s', Machine='%s' WHERE idmd5 = %d" % (Status, current_time, machined, block_id)
    
    cursor.execute(sqltime)
    cnx.commit()
    cursor.close()
    

def insert_data(hash, plaintext):
    #cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='hash')
    cursor = cnx.cursor()
    h=re.escape(joined)
    #print (h)
    SQL = "INSERT INTO table_%c(Hash, Plaintext) VALUES ('%s', '%s')" % (hexed[0], hexed, h)
    #print (SQL)
    
    try:
        cursor.execute(SQL)
        print ("Added Record :", hexed, joined)
        cnx.commit()
    except:
        print ("Error")
        cnx.rollback()
    cursor.close()
    

xx=0
while xx < 100:
    cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='hash')
    get = block()
    keyspace = (ascii_letters + digits + punctuation)
    keyspace2 = product(keyspace, repeat = get[3])
    update_block(get[2], 'Going')
    count = 0
    start = get[0]
    stop = get[1]
    #cursor = cnx.cursor()
    for x in islice(keyspace2, start, stop):
        joined = (''.join(x))
        hexed = hashlib.md5((joined).encode()).hexdigest()
        parms = [(hexed), (joined)]
        print (joined, (get[1] - get[0] - count))
        count += 1
        cursor = cnx.cursor()
        h=re.escape(joined)
    #print (h)
        #SQL = "INSERT INTO table_%c(Hash, Plaintext) VALUES ('%s', '%s')" % (hexed[0], hexed, h)
    #print (SQL)
        
        try:
            #cursor.execute(SQL)
            print ("Added Record :", hexed, joined)
            cnx.commit()
        except:
            print ("Error")
            cnx.rollback()
        cursor.close()
        #insert_data(hexed, joined)
        for k in parms:
            print parms[k]
    if count > 9999:
        update_block(get[2], 'Done')
    else:
        update_block(get[2], 'Error')
    cnx.close()
    xx += 1

cnx.close()

